{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "25790308",
   "metadata": {},
   "source": [
    "## IIR滤波器设计脉冲响应不变法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e80ceba",
   "metadata": {},
   "source": [
    "利用脉冲响应不变变换法，把下列模拟域的模拟滤波器转换为数字滤波器，采样周期$T=1$\n",
    "$$H(s)=\\frac{s+1}{s^2+5s+6}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "418fb590",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 1.       , -0.2208835]),\n",
       " array([ 1.        , -0.18512235,  0.00673795]))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#library\n",
    "import numpy as np\n",
    "from scipy import signal\n",
    "\n",
    "#analog filter transfer function\n",
    "b_analog = np.array([1,1]) #numerator\n",
    "a_analog = np.array([1,5,6]) #denominator\n",
    "T = 1 #sampling period\n",
    "\n",
    "#impulse response invariable method\n",
    "r,p_analog,k = signal.residue(b_analog,a_analog) \n",
    "#r is coefficient, p_analog is pole in analog\n",
    "p_digital = np.exp(p_analog*T) #p_digital is pole in digital\n",
    "a_digital,b_digital = signal.invresz(r,p_digital,k)\n",
    "a_digital,b_digital #display values of denominator and numerator"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e92f61d",
   "metadata": {},
   "source": [
    "根据打印结果，可以得到该模拟域滤波器对应的Z域传递函数为\n",
    "$$H(z)=\\frac{1-0.2208835z^{-1}}{1-0.18512235z^{-1}+0.00673795z^{-2}}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36610644",
   "metadata": {},
   "source": [
    "invresz：Compute b(z) and a(z) from partial fraction expansion.\n",
    "函数用法：b,a = scipy.signal.invresz(r, p, k, tol=0.001, rtype='avg')\n",
    "输入参数：r是对应极点的余数；p是极点，相等的极点必须相邻；k是系数；tol是两根被认为相等前的距离容差，默认为0.001；rtype是当多个根之间距离小于容差tol时，最后呈现的方式，默认为avg，。\n",
    "输出参数：b是数字滤波器的分子系数；a是数字滤波器的分母系数。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
